// 页面公共函数
import router from '@/router/index'
import { useI18n } from 'vue-i18n'
import { useMainStore } from '@/store'
import dayjs from '@/plugin/dayjs/index'
/**
 * 获取配置
 */
export const _getConfig = (key) => {
  let value = ''
  if (key) {
    value = window.__config[key] || ''
  }
  return value
}
/**
 * 页面跳转
 * @param {*} path
 * @param {*} parmas
 */
export const _toView = (path, params = {}) => {
  if (path) {
    router.push(path)
  } else if (params.path || params.name) {
    router.push(params)
  }
}

/**
 * 页面跳转
 * @param {*} path
 */
export const _toReplace = (path) => {
  if (path) {
    router.replace(path)
  }
}
/**
 * 页面后退
 */
export const _back = (num = -1) => {
  if (window.history.length <= 1) {
    _toReplace('/')
  } else {
    router.go(num)
  }
}

/**
 * 多语言 平台定制化
 * @param {*} path
 * @param {*} parmas
 */
export const _t18 = (name, platform = [], i18n = null) => {
  if (!i18n) {
    i18n = useI18n()
  }
  if (platform.includes(__config._APP_ENV)) {
    return i18n.t(`${__config._APP_ENV}_${name}`)
  } else {
    return i18n.t(name)
  }
}

/**
 * 千分位金额格式化
 * @param {*} val
 * @returns
 */
export const _numberWithCommas = (val) => {
  if (!isNaN(Number(val))) {
    const parts = `${val}`.split('.')
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
    return parts.join('.')
  }
  return val
}

/**
 * 时间格式化 KLine
 */
export const _klineTimeFormat = (time, expression = '', flag = false) => {
  const mainStore = useMainStore()
  let tempExpression = mainStore.timeFormat
  if (flag) {
    tempExpression = expression || tempExpression
  } else {
    tempExpression = tempExpression + ` ${expression || 'HH:mm:ss'}`
  }
  return dayjs(time).tz('utc').format(tempExpression)
}

/**
 * 买涨买跌 规则
 * @param {*} open 开盘价
 * @param {*} close 关盘价
 * @param {*} direction 方向 buy 买(默认) sell 卖
 * @param {*} default 默认
 * @returns rise 涨 fall 跌 draw 平
 */
export const _isRFD = (open, close, direction = 'buy', def = 'draw') => {
  let tempVal = ''
  open = Number(open)
  close = Number(close)
  if (isNaN(open) || isNaN(close)) {
    return 'draw'
  }
  if (open < close) {
    // buy 涨 sell 跌D
    if (direction == 'buy') {
      tempVal = 'rise'
    } else if (direction == 'sell') {
      tempVal = 'fall'
    }
  } else if (open > close) {
    // buy 跌 sell 涨
    if (direction == 'buy') {
      tempVal = 'fall'
    } else if (direction == 'sell') {
      tempVal = 'rise'
    }
  } else if (open == close) {
    // buy sell 平
    tempVal = def
  }

  return tempVal
}


